<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" fullscreen center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="编码"></el-input>
      </el-form-item>
      <el-form-item label="产地地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="产地地址"></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话号码"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="mobilePhone">
        <el-input v-model="dataForm.mobilePhone" placeholder="手机号码"></el-input>
      </el-form-item>
      <el-form-item label="许可证编号" prop="permitNumber">
        <el-input v-model="dataForm.permitNumber" placeholder="许可证编号"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio label="1">启用</el-radio>
          <el-radio label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="memo">
        <el-input v-model="dataForm.memo" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="ischeck">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getEimsSupplierStockObj, addEimsSupplierStockObj, putEimsSupplierStockObj } from '@/api/eims/supplierstock'

  export default {
    data() {
      return {
        visible: false,
        ischeck: false,
        dataForm: {
          id: 0,
          name: '',
          code: '',
          address: '',
          phone: '',
          mobilePhone: '',
          permitNumber: '',
          status: '1',
          memo: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
          tenantId: '',
          orgId: '',
          unitId: '',
          delFlag: ''
        },
        dataRule: {
          name: [{
            required: true,
            message: '名称不能为空',
            trigger: 'blur'
          }],
          code: [{
            required: true,
            message: '编码不能为空',
            trigger: 'blur'
          }],
          address: [{
            required: true,
            message: '产地地址不能为空',
            trigger: 'blur'
          }],
          phone: [{
            required: true,
            message: '电话号码不能为空',
            trigger: 'blur'
          }],
          mobilePhone: [{
            required: true,
            message: '手机号码不能为空',
            trigger: 'blur'
          }],
          permitNumber: [{
            required: true,
            message: '许可证编号不能为空',
            trigger: 'blur'
          }],
          status: [{
            required: true,
            message: '状态(1-启用,0-禁用)不能为空',
            trigger: 'blur'
          }],
          memo: [{
            required: true,
            message: '备注不能为空',
            trigger: 'blur'
          }],
          createTime: [{
            required: true,
            message: '创建时间不能为空',
            trigger: 'blur'
          }],
          updateTime: [{
            required: true,
            message: '修改时间不能为空',
            trigger: 'blur'
          }],
          createUser: [{
            required: true,
            message: '创建用户id不能为空',
            trigger: 'blur'
          }],
          modifyUser: [{
            required: true,
            message: '修改用户id不能为空',
            trigger: 'blur'
          }],
          tenantId: [{
            required: true,
            message: '租户id不能为空',
            trigger: 'blur'
          }],
          orgId: [{
            required: true,
            message: '组织id不能为空',
            trigger: 'blur'
          }],
          unitId: [{
            required: true,
            message: '单位id不能为空',
            trigger: 'blur'
          }],
          delFlag: [{
            required: true,
            message: '逻辑删除(1-正常,-1已删除)不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getEimsSupplierStockObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putEimsSupplierStockObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              addEimsSupplierStockObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
            this.ischeck = false
          }
        })
      }
    }
  }
</script>
